/*********************************************************
	Project:		 
	File name:	define.h	
	Write:		 
	Time:		 
*********************************************************/
#ifndef _DEFINE_H_
#define _DEFINE_H_

#define SPTINY @tiny
#define SPNEAR @near
#define SPFAR  @far

#define NOP		_asm("nop");/* No Operation */
#define DI		_asm("sim")/* disable interrupts */
#define DisableInterrupts	DI
#define EI		_asm("rim")/* enable interrupts */
#define EnableInterrupts	EI
#define TRAP  _asm("trap");/* Trap (soft IT) */
#define WAIT _asm("wfi");/* Wait For Interrupt */
#define HALT  _asm("halt");        /* Halt */


#define	M_WAIT	{NOP;NOP;NOP;NOP;NOP;\
				NOP;NOP;NOP;NOP;NOP;\
				NOP;NOP;NOP;NOP;NOP;\
				NOP;NOP;NOP;NOP;NOP;\
				NOP;NOP;NOP;NOP;NOP;\
				NOP;NOP;}
#define	M_WAIT_1US	{NOP;NOP;NOP;NOP;NOP;\
					NOP;NOP;NOP;NOP;NOP;\
					NOP;NOP;}


typedef unsigned char bool;
typedef unsigned char uchar;
typedef unsigned char int8u;
typedef unsigned int uword;
typedef uchar * puchar;
typedef uword * puword;
typedef uchar EVENT_ID ;
typedef void (* pfunction) (uchar);     

typedef unsigned long  u32;
typedef unsigned short u16;
typedef unsigned char  u8;
typedef unsigned long  const uc32;  /* Read Only */
typedef unsigned short const uc16;  /* Read Only */
typedef unsigned char  const uc8;   /* Read Only */
typedef volatile unsigned long  vu32;
typedef volatile unsigned short vu16;
typedef volatile unsigned char  vu8;
typedef volatile unsigned long  const vuc32;  /* Read Only */
typedef volatile unsigned short const vuc16;  /* Read Only */
typedef volatile unsigned char  const vuc8;   /* Read Only */

#define SetBit(Var,Place) (Var|=(1<<(Place&0x7)))
#define ClrBit(Var,Place) (Var&=(1<<(Place&0x7))^255)
#define GetBit(Var,Place) ((Var& (1<<(Place&0x7)))==0?0:1)
#define CplBit(Var,Place) (Var^=(1<<(Place&0x7)))
#define LSB(WORD)		  (uchar)WORD
#define MSB(WORD)		  (uchar)(WORD>>8)
#define WORD(MSB,LSB)	  (uword)(((uword) MSB << 8) + LSB)

#define SetLBit(Var,Place) (Var|=(1<<(Place&0xf)))
#define ClrLBit(Var,Place) (Var&=(1<<(Place&0xf))^0xffff)
#define GetLBit(Var,Place) ((Var& (1<<(Place&0xf)))==0?0:1)

#define HighNibble(var)  (var>>4)
#define LowNibble(var)   (var&0x0f)
#define _BYTE(NibbleH,NibbleL)  ((NibbleH<<4)|NibbleL)

#define BITN_0			0
#define BITN_1			1
#define BITN_2			2
#define BITN_3			3
#define BITN_4			4
#define BITN_5			5
#define BITN_6			6
#define BITN_7			7

#define BIT0			0x01
#define BIT1			0x02
#define BIT2			0x04
#define BIT3			0x08
#define BIT4			0x10
#define BIT5			0x20
#define BIT6			0x40
#define BIT7			0x80
#define		Cpl_Bit(Val,Place)		((Val)^=(Place))
#define		Get_Bit(Val,Place)		((Val)&(Place))
#define		Set_Bit(Val,Place)		((Val)|=(Place))
#define		Clr_Bit(Val,Place)		((Val)&=(~(Place)))


#define BYTE_0(n)                 ((u8)((n) & (u8)0xFF))        /*!< Returns the low byte of the 32-bit value */
#define BYTE_1(n)                 ((u8)(BYTE_0((n) >> (u8)8)))  /*!< Returns the second byte of the 32-bit value */
#define BYTE_2(n)                 ((u8)(BYTE_0((n) >> (u8)16))) /*!< Returns the third byte of the 32-bit value */
#define BYTE_3(n)                 ((u8)(BYTE_0((n) >> (u8)24))) /*!< Returns the high byte of the 32-bit value */

#define MAX(x,y)	( ((x) > (y)) ? (x) : (y) )
typedef union 
{
	uchar	b_var[2]; 
	uword	w_var;
}WORDVAR;

typedef enum
{
	OFF=0,
	ON=1
}ONOFF;
#define false 0
#define true  1
#define NONE  0

typedef enum {
   RESET = 0,
   SET = !RESET
}FlagStatus, ITStatus;
typedef enum {
  DISABLE = 0,
  ENABLE = !DISABLE
}FunctionalState;

typedef struct{
	uword x;
	uword y;
}TS_POSITION;
ext  TS_POSITION	Touch_Key;

#define NULL      ((void *) 0)

#define BEGIN_IDWG		IWDG_KR=0xCC;
#define CLEAR_IDWG		IWDG_KR=0xAA;
#define ACCESS_IDWG		IWDG_KR=0x55;
//Watch Dog is enabled
#define BEGIN_WWDG	{WWDG_WR=0X7F;WWDG_CR=0XFF;}
#define CLEAR_WWDG	WWDG_CR=0X7F;

//tick define 
#define T100_10S	100

#define T200MS_100  2
#define T20S_100    200
#define T6S_100     60

#define	T5S_32		157
#define	T2S_32		63
#define T200MS_4	50
#define T100MS_4	25
#define T60MS_4		15
//<<<

#define IO_AS_INPUT				0	//DDR
#define IO_AS_OUTPUT			1	//DDR
#define FLOATING_INPUT			0	//CR1
#define PULL_UP_INPUT			1	//CR1
#define OPEN_DRAIN_OUT			0	//CR1
#define PUSH_PULL_OUT			1	//CR1
#define INPUT_INTERRUPT_DISANBLED	0	//CR2
#define INPUT_INTERRUPT_ENANBLED	1	//CR2
#define OUTPUT_LIMIT_2MHZ			0	//CR2 
#define OUTPUT_LIMIT_10MHZ			1	//CR2

#define ADC_OFF       ADC_CR1_ADON=OFF;
#define ADC_ON        ADC_CR1_ADON=ON;

#define MPEG_RESET_LEVEL	HIGH_LEVEL


#define FLASH_UNLOCK_KEY1   0x56
#define FLASH_UNLOCK_KEY2   0xAE
#define RESET_BATT_FAIL     0xC3
#define RESET_BATT_OK       0x3C


#define asm_nop()  NOP;
#define asm_read(x) _asm ("ld a,x"); 



#endif  //_DEFINE_H_

